﻿Imports VendasBLL
Public Class ConsultaAgendamentosFRM
    Dim ObjBLL As New AgendamentoBLL
    Public Tipo As String
    Public ObjA As AgendamentoBLL
#Region "Carrega grid"
    Sub CarregaGrid()
        DGVDados.DataSource = ObjBLL.buscar(FormatDateTime(Date.Now, DateFormat.ShortDate).ToString(), 7)
        DGVDados.Refresh()
    End Sub
#End Region

#Region "ComboBox Filtro"
    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBFiltro.KeyPress
        e.Handled = True
    End Sub
#End Region

#Region "Double click do datagrid"
    Private Sub DGVDados_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        Try

            If Tipo = "ret" Then
                Dim indice As Integer = -1
                'Percorre O DataGridView Verificando a Linha selecionada
                For Each linha As DataGridViewRow In DGVDados.SelectedRows
                    If (Not linha.Cells("age_codigo") Is Nothing) Then
                        'Guarda A Posição Selecionada
                        indice = linha.Index
                    End If
                Next
                If (indice <> -1) Then
                    If (DGVDados.Rows(indice).Cells("sta_codigo").Value = 1) Then
                        If MessageBox.Show("Deseja tornar o agendamento código: " & DGVDados.Rows(indice).Cells("age_codigo").Value & " em uma ordem de serviço?", "", MessageBoxButtons.YesNo) = vbYes Then
                            ObjA = New AgendamentoBLL(Val(DGVDados.Rows(indice).Cells("age_codigo").Value.ToString()))
                            Me.Close()

                        End If

                    Else
                        Throw New Exception("Agendamento inativo")
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
#End Region
#Region "Localizar no textbox"
    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        Select Case CBFiltro.Text
            Case "Código"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Cliente"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Placa"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Data"
                BBuscar_Click(sender, e)
            Case "Status"
                DGVDados.DataSource = ObjBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
        End Select
        If TXTLocalizar.Text = "" Then
            DGVDados.DataSource = ObjBLL.buscar(FormatDateTime(Date.Now, DateFormat.ShortDate).ToString(), 7)
        End If
        DGVDados.Refresh()
    End Sub

#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBVoltar.Click
        ObjA = Nothing
        Me.Close()
    End Sub
#End Region

#Region "Keypress Localizar"
    Private Sub TXTLocalizar_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTLocalizar.KeyPress, TXTLocalizar.KeyPress
        If CBFiltro.SelectedIndex = 0 Then
            If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
                e.Handled = True
            End If
        End If
    End Sub
#End Region

#Region "ComboboxFiltro SelectedIndex"
    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        If CBFiltro.SelectedIndex = 4 Then
            CBStatus.Visible = True
            TXTLocalizar.Visible = False
            CBStatus.SelectedIndex = 0
            CBStatus_SelectedIndexChanged(sender, e)
        Else
            CBStatus.Visible = False
            TXTLocalizar.Visible = True
            CarregaGrid()
        End If
        If CBFiltro.SelectedIndex = 3 Then
            TXTLocalizar.Visible = False
            DTPInicial.Visible = True
            DTPFinal.Visible = True
            LBInformacao.Visible = True
            BBuscar.Visible = True
        Else
            BBuscar.Visible = False
            DTPInicial.Visible = False
            DTPFinal.Visible = False
            LBInformacao.Visible = False
        End If
        TXTLocalizar.Text = ""

    End Sub
#End Region

#Region "ComboboxStatus Selectedindex"
    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        DGVDados.DataSource = ObjBLL.buscar(CBStatus.Text, CBFiltro.SelectedIndex)
    End Sub
#End Region

#Region "Buscar por datas"
    Private Sub BBuscar_Click(sender As System.Object, e As System.EventArgs) Handles BBuscar.Click
        Try
            DGVDados.DataSource = ObjBLL.locdata(DTPInicial.Text, DTPFinal.Text)
            DGVDados.Refresh()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region

#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region

    Private Sub ConsultaAgendamentosFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CarregaGrid()
        'Determinar o começo o filtro como descrição
        CBFiltro.SelectedIndex = 1
        CBFiltro.Focus()
    End Sub

    Private Sub TSBCadastrar_Click(sender As System.Object, e As System.EventArgs) Handles TSBNovo.Click
        Try
            AgendamentoFRM.Op = "I"
            TSBNovo.BackColor = Color.Gainsboro
            AgendamentoFRM.ShowDialog()
            CarregaGrid()
            CarregaGrid()
        Catch ex As Exception
        Finally
            TSBNovo.BackColor = Color.Transparent
        End Try
    End Sub
End Class